home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 3 / Info_Mac_1994-01.iso / Development / Source / Macintosh Tracker 1.1 Source / Tracker Server Folder / mac_hack.c < prev    next >
C/C++ Source or Header  |  1993-07-07  |  11KB  |  483 lines

  1. /* mac_hack.c */
  2.  
  3. /* All the stuff in this file was written by Thomas R. Lawrence. */
  4. /* See the "mac_readme" or "mac_programmer_info" files for more information */
  5. /* about the Macintosh port */
  6.  
  7. #include "mac_hack.h"
  8. #include "mac_event.h"
  9. #include <console.h>
  10. #include <unix.h>
  11.  
  12. #include <Aliases.h>
  13. #include <SANE.h>
  14. #include <GestaltEqu.h>
  15. #include <Power.h>
  16.  
  17.  
  18. /* stuff from mac_event.c */
  19. extern FSSpec                        GlobalFileSpec;
  20. extern char                            FakeKeyBuffer[MAXKEYS];
  21. extern int                            KeyBufPtr;
  22. extern Boolean                    QuitPending;
  23. extern short                        Pausing;
  24. extern Boolean                    ReceivedOpenEventFlag;
  25.  
  26. extern short                        AntiAliasing;
  27. extern short                        StereoOn;
  28. extern unsigned short        SamplingRate;
  29. extern short                        NumRepeats;
  30. extern short                        Speed;
  31. extern short                        StereoMix;
  32. extern short                        Loudness;
  33.  
  34.  
  35. #define VOLUMEINCREMENT (8)
  36.  
  37. #define SMALL_MAX_TIME_SLICE (10)
  38. #define LARGE_MAX_TIME_SLICE (120)
  39. #define WHATISBREATHINGSPACE (48) /* about 16 seconds at 22254 Hz */
  40.  
  41. long                                        PowerManagerInfo = 0;
  42.  
  43.  
  44.  
  45. BOOL      run_in_fg(void)
  46.   {
  47.     return TRUE;
  48.   }
  49.  
  50.  
  51. void      sane_tty(void)
  52.   {
  53.   }
  54.  
  55.  
  56. void      nonblocking_io(void)
  57.   {
  58.   }
  59.  
  60. void            ReenableIdleState(void)
  61.     {
  62.         if ((PowerManagerInfo & (1 << gestaltPMgrExists)) != 0)
  63.             {
  64.                 EnableIdle();
  65.             }
  66.     }
  67.  
  68.  
  69. #define MAXCOUNT (3) /* every MAXCOUNTth call to may_getchar actually does something */
  70.  
  71. static unsigned long        LastEventTime = 0;
  72. static short                        Count = MAXCOUNT;
  73.  
  74. /* this is called by the tracker code to get keypresses from the user. */
  75. /* we use it to manage use of processor. */
  76. int        may_getchar(void)
  77.   {
  78.      LoopPoint:
  79.         if (QuitPending)
  80.             {
  81.                 end_all();
  82.             }
  83.  
  84.         if (Pausing)
  85.             {
  86.                 goto DoThingPoint;
  87.             }
  88.  
  89.         if (Count < 0)
  90.             {
  91.                 Count = MAXCOUNT;
  92.             }
  93.          else
  94.             {
  95.                 Count -= 1;
  96.                 return EOF;
  97.             }
  98.  
  99.          if (NumberPendingBlocks() < WHATISBREATHINGSPACE)
  100.              {
  101.                  /* if we don't have very many blocks prepared, then we don't call */
  102.                  /* the event routine for a long time.  In other words, we give */
  103.                  /* ourselves a LARGE_MAX_TIME_SLICE */
  104.                  if (TickCount() - LastEventTime > LARGE_MAX_TIME_SLICE)
  105.                      {
  106.                       DoThingPoint:
  107.                          WaitForEvent(0);
  108.                          LastEventTime = TickCount();
  109.                      }
  110.              }
  111.          else
  112.             {
  113.                 /* otherwise, we grant ourselves a SMALL_MAX_TIME_SLICE during */
  114.                 /* which to construct more blocks */
  115.                  if (TickCount() - LastEventTime > SMALL_MAX_TIME_SLICE)
  116.                      {
  117.                          goto DoThingPoint;
  118.                      }
  119.             }
  120.  
  121.         /* extract 'keypresses' from our buffer.  First we handle our own */
  122.         /* special volume controls and our special pause function, and if the */
  123.         /* key wasn't one of those, we pass it back to the caller to be handled. */
  124.         if (KeyBufPtr > 0)
  125.             {
  126.                 int                    Scan;
  127.                 int                    KeyTemp;
  128.  
  129.                 KeyTemp = (unsigned char)FakeKeyBuffer[0];
  130.                 for (Scan = 1; Scan < KeyBufPtr; Scan += 1)
  131.                     {
  132.                         FakeKeyBuffer[Scan - 1] = FakeKeyBuffer[Scan];
  133.                     }
  134.                 KeyBufPtr -= 1;
  135.                 if (KeyTemp == '+')
  136.                     {
  137.             Loudness += VOLUMEINCREMENT;
  138.             if (Loudness > 255)
  139.               {
  140.                 Loudness = 255;
  141.               }
  142.             ResetVolumeTable();
  143.             goto LoopPoint;
  144.           }
  145.         if (KeyTemp == '-')
  146.             {
  147.             Loudness -= VOLUMEINCREMENT;
  148.             if (Loudness < 0)
  149.               {
  150.                 Loudness = 0;
  151.               }
  152.             ResetVolumeTable();
  153.             goto LoopPoint;
  154.           }
  155.         if (KeyTemp == ' ')
  156.             {
  157.                 TogglePause();
  158.                 goto LoopPoint;
  159.             }
  160.         if (KeyTemp == '<')
  161.             {
  162.                 /* fudge rewind into restart, for consistency */
  163.                 KeyTemp = 'r';
  164.             }
  165.         if ((KeyTemp == '>') || (KeyTemp == '|'))
  166.             {
  167.                 /* these don't work well in this version because it doesn't run */
  168.                 /* at interrupt level, but instead precomputes the song as far */
  169.                 /* ahead as possible.  This usually means, on a fast computer, that */
  170.                 /* you don't hear the result of clicking the button until 5 or */
  171.                 /* 10 seconds later.  So I just block them for the time being.  If */
  172.                 /* I or someone else hacks this to run at interrupt time, then */
  173.                 /* we can put these back in. */
  174.                 goto LoopPoint;
  175.             }
  176.                 return KeyTemp;
  177.             }
  178.          else
  179.              {
  180.                  return EOF;
  181.              }
  182.   }
  183.  
  184.  
  185. /* this is the real main function.  We basically wait for the open event, */
  186. /* then using the parameters sent to use, we construct a fake little command line */
  187. /* string which is passed to the tracker's main function.  It never knows */
  188. /* what happened. */
  189. #undef main
  190. void        main(void)
  191.   {
  192.     int                            argc;
  193.     char*                        argv[14];
  194.     OSErr                        Error;
  195.     long                        ProcTypeInfo;
  196.     char                        SamplingRateString[20];
  197.     char                        RepeatsString[20];
  198.     char                        SpeedString[20];
  199.     char                        MixString[20];
  200.  
  201.         MaxApplZone();
  202.         InitGraf(&thePort);
  203.         InitFonts();
  204.         FlushEvents(everyEvent,0);
  205.         InitWindows();
  206.         InitMenus();
  207.         TEInit();
  208.         InitDialogs(NULL);
  209.         InitCursor();
  210.  
  211.         if (!RegisterEventHandlers())
  212.             {
  213.                 return;
  214.             }
  215.  
  216.         Error = Gestalt(gestaltPowerMgrAttr,&PowerManagerInfo);
  217.         if (Error != noErr)
  218.             {
  219.                 return;
  220.             }
  221.         if ((PowerManagerInfo & (1 << gestaltPMgrExists)) != 0)
  222.             {
  223.                 DisableIdle();
  224.                 atexit(&ReenableIdleState);
  225.             }
  226. #if __option(mc68020)
  227.         Error = Gestalt(gestaltProcessorType,&ProcTypeInfo);
  228.         if ((Error != noErr) || (ProcTypeInfo == gestalt68000))
  229.             {
  230.                 FatalError(FatalError68020NeededID);
  231.                 return;
  232.             }
  233. #endif
  234.  
  235. #ifdef AskForFile
  236.         {
  237.             Point                                Thing = {50,50};
  238.             StandardFileReply        R;
  239.  
  240.             /* this is only used for debugging when we don't have access to the interface */
  241.             /* program (since the interface program can't link to this program when it */
  242.             /* is being run under the debugger.) */
  243.             StandardGetFile(NULL,-1,NULL,&R);
  244.             GlobalFileSpec = R.sfFile;
  245.         }
  246. #else
  247.         /* waiting for open document command to come */
  248.         while (!ReceivedOpenEventFlag && !QuitPending)
  249.             {
  250.                 WaitForEvent(60);
  251.             }
  252.         if (QuitPending)
  253.             {
  254.                 return;
  255.             }
  256. #endif
  257.  
  258.         /* constructing parameter list */
  259.         argc = 14;
  260.         argv[0] = "tracker";
  261.         argv[1] = "-oversample";
  262.         if (AntiAliasing)
  263.             {
  264.                 argv[2] = "2";
  265.             }
  266.          else
  267.             {
  268.                 argv[2] = "1";
  269.             }
  270.         if (StereoOn)
  271.             {
  272.                 argv[3] = "-stereo";
  273.             }
  274.          else
  275.             {
  276.                 argv[3] = "-mono";
  277.             }
  278.         argv[4] = "-frequency";
  279.         argv[5] = SamplingRateString; sprintf(SamplingRateString,"%d",(int)SamplingRate);
  280.         argv[6] = "-repeats";
  281.         argv[7] = RepeatsString; sprintf(RepeatsString,"%d",(int)NumRepeats);
  282.         argv[8] = "-speed";
  283.         argv[9] = SpeedString; sprintf(SpeedString,"%d",(int)Speed);
  284.         argv[10] = "-mix";
  285.         argv[11] = MixString; sprintf(MixString,"%d",(int)StereoMix);
  286.         argv[12] = "-tolerant";
  287.         argv[13] = "FileName";
  288.  
  289.     main2(argc,argv);
  290.   }
  291.  
  292.  
  293. void* popen(char* pipe, char* Mode)
  294.   {
  295.     perror("Macintosh doesn't have pipes!");
  296.     FatalError(FatalErrorCantOpenCompressedFiles);
  297.     end_all();
  298.   }
  299.  
  300.  
  301. void pclose(FILE* file)
  302.   {
  303.   }
  304.  
  305.  
  306.  
  307.  
  308. #include "defs.h"
  309. #include "extern.h"
  310. #include "song.h"
  311. #include "channel.h"
  312.  
  313.  
  314.  
  315.  
  316.  
  317. int MYprintf(...)
  318.     {
  319.     }
  320.  
  321. int MYfprintf(...)
  322.     {
  323.     }
  324.  
  325. int MYfgetc(FILE* FileToGetFrom)
  326.     {
  327.         unsigned char            Temp;
  328.         long                            Length;
  329.         OSErr                            Error;
  330.  
  331.         if (FileToGetFrom->BufPtr == FILEBUFFERSIZE)
  332.             {
  333.                 FileToGetFrom->BufPtr = 0;
  334.                 Length = FILEBUFFERSIZE;
  335.                 SetFPos(FileToGetFrom->MacFileHandle,fsFromStart,FileToGetFrom->Index);
  336.                 Error = FSRead(FileToGetFrom->MacFileHandle,&Length,
  337.                     &(FileToGetFrom->Buffer[0]));
  338.             }
  339.         Temp = FileToGetFrom->Buffer[FileToGetFrom->BufPtr];
  340.         FileToGetFrom->Index += 1;
  341.         FileToGetFrom->BufPtr += 1;
  342.         if (FileToGetFrom->Index >= FileToGetFrom->EndOfFile)
  343.             {
  344.                 return EOF;
  345.             }
  346.          else
  347.             {
  348.                 return Temp;
  349.             }
  350.     }
  351.  
  352. int MYfputc(int CharToPut, FILE *FileToPutTo)
  353.     {
  354.     }
  355.  
  356. FILE* MYfopen(char* FileName, char* Mode)
  357.     {
  358.         OSErr                Error;
  359.         FILE*                Temp;
  360.         short                FileHandleTemp;
  361.  
  362.         Error = FSpOpenDF(&GlobalFileSpec,fsCurPerm,&FileHandleTemp);
  363.         if (Error != noErr)
  364.             {
  365.                 BUG("\pError opening file in MYfopen");
  366.                 FatalError(FatalErrorCouldntOpenFile);
  367.                 end_all();
  368.                 /* return NULL; */
  369.             }
  370.          else
  371.             {
  372.                 Temp = (FILE*)NewPtr(sizeof(FILE));
  373.                 Temp->MacFileHandle = FileHandleTemp;
  374.                 Temp->Index = 0;
  375.                 GetEOF(FileHandleTemp,&(Temp->EndOfFile));
  376.                 Temp->BufPtr = FILEBUFFERSIZE; /* force refill upon first read */
  377.                 return Temp;
  378.             }
  379.     }
  380.  
  381. int MYfclose(FILE* FileToClose)
  382.     {
  383.         OSErr                Error;
  384.  
  385.         if (FileToClose != NULL)
  386.             {
  387.                 Error = FSClose(FileToClose->MacFileHandle);
  388.                 if (Error != noErr)
  389.                     {
  390.                         FatalError(FatalErrorCouldntCloseFile);
  391.                         BUG("\pError closing file in MYfclose");
  392.                     }
  393.                 DisposPtr((Ptr)FileToClose);
  394.             }
  395.     }
  396.  
  397. int MYfread(char* PlaceToPut, int SizeOfElement, int NumElements, FILE* TheFile)
  398.     {
  399.         long        NumBytesDone;
  400.         OSErr        LastError;
  401.         long        NumberOfBytes;
  402.         long        LengthTemp;
  403.  
  404.         SetFPos(TheFile->MacFileHandle,fsFromStart,TheFile->Index);
  405.         NumberOfBytes = SizeOfElement * NumElements;
  406.         TheFile->Index += NumberOfBytes;
  407.      LoopPoint:
  408.         NumBytesDone = NumberOfBytes;
  409.         LastError = FSRead(TheFile->MacFileHandle,&NumBytesDone,PlaceToPut);
  410.         PlaceToPut += NumBytesDone;
  411.         NumberOfBytes -= NumBytesDone;
  412.         if ((LastError == noErr) && (NumberOfBytes != 0)) goto LoopPoint;
  413.         if (LastError != noErr)
  414.             {
  415.                 BUG("\pError reading from file in MYfread");
  416.             }
  417.         TheFile->BufPtr = TheFile->Index & (FILEBUFFERSIZE - 1);
  418.         SetFPos(TheFile->MacFileHandle,fsFromStart,TheFile->Index - TheFile->BufPtr);
  419.         LengthTemp = FILEBUFFERSIZE;
  420.         FSRead(TheFile->MacFileHandle,&LengthTemp,&(TheFile->Buffer[0]));
  421.         return LastError;
  422.     }
  423.  
  424. void* MYmalloc(long SizeOfBlock)
  425.     {
  426.         void*            Block;
  427.  
  428.         Block = NewPtrClear(SizeOfBlock);
  429.         if (Block == NULL)
  430.             {
  431.                 FatalError(FatalErrorOutOfMemory);
  432.                 end_all();
  433.             }
  434.         return Block;
  435.     }
  436.  
  437. void MYfree(void* Block)
  438.     {
  439.         DisposPtr(Block);
  440.     }
  441.  
  442. void *MYcalloc(size_t NumThings, size_t SizeOfThing)
  443.   {
  444.       return malloc((NumThings + 1) * SizeOfThing); /* extra for antialiasing byte */
  445.   }
  446.  
  447. #undef exit
  448. void MYexit(int Value)
  449.     {
  450.         /* ExitToShell(); */
  451.         exit(Value); /* we DO want to call "atexit"ed things, such as powerbook idle fixer */
  452.     }
  453.  
  454. char* MYgetenv(char* MeaninglessParameter)
  455.     {
  456.         return NULL;
  457.     }
  458.  
  459. void MYperror(char* ErrorMessage)
  460.     {
  461.     }
  462.  
  463. void MYputs(char* Message)
  464.     {
  465.     }
  466.  
  467. long double MYfloor(long double Base)
  468.     {
  469.         return (long int)Base;
  470.     }
  471.  
  472. long double MYpow(long double Base, long double Exponent)
  473.     {
  474.         extended            ExtBase,ExtExponent,ExtResult;
  475.         long double        DblResult;
  476.  
  477.         x96tox80(&Base,&ExtBase);
  478.         x96tox80(&Exponent,&ExtExponent);
  479.         ExtResult = power(ExtBase,ExtExponent);
  480.         x80tox96(&ExtResult,&DblResult);
  481.         return DblResult;
  482.     }
  483.